package com.example.myapplication.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.adapter.HomeMainNewsTypeAdapter;
import com.example.myapplication.adapter.HomeMainRecommendAdapter;
import com.example.myapplication.adapter.HomeMainSpecialAdapter;
import com.example.myapplication.base.BaseFragment;
import com.example.myapplication.base.Constant;
import com.example.myapplication.bean.MainHomeBannerBean;
import com.example.myapplication.bean.MainHomeNewsTypeBean;
import com.example.myapplication.bean.MainHomeRecommendBean;
import com.example.myapplication.bean.MainHomeSpecialBean;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.io.UnsupportedEncodingException;

public class HomeFragment extends BaseFragment {

    private Banner mainBanner;
    private RecyclerView recommendRecyclerView;
    private RecyclerView specialRecyclerView;
    private RecyclerView newsTypeRecyclerView;

    @Override
    public View initView() {

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, null);
        mainBanner = view.findViewById(R.id.mainBanner);
        recommendRecyclerView = view.findViewById(R.id.recommendRecyclerView);
        specialRecyclerView = view.findViewById(R.id.specialRecyclerView);
        newsTypeRecyclerView = view.findViewById(R.id.newsTypeRecyclerView);
        return view;

    }

    @Override
    public void initData() {

        getBanner();//取首页轮播
        getRecommendService();//取推荐服务
        getSpecial();//取首页专题
        getNewsType();//取新闻分类

    }

    @Override
    public void initEvent() {

    }

    //自定义方法

    private void getBanner() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.URL_API + "rotation/list?type=2", response -> {
            MainHomeBannerBean bean = new Gson().fromJson(response, MainHomeBannerBean.class);
            mainBanner.setAdapter(new BannerImageAdapter<MainHomeBannerBean.RowsDTO>(bean.getRows()) {
                @Override
                public void onBindView(BannerImageHolder holder, MainHomeBannerBean.RowsDTO data, int position, int size) {
                    RequestOptions requestOptions = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(16));
                    Glide.with(holder.itemView).load(Constant.URL + data.getAdvImg()).apply(requestOptions).into(holder.imageView);
                }
            });
        }, error -> {

        });
        queue.add(stringRequest);

    }

    private void getRecommendService() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.URL_API + "service/list?isRecommend=N", response -> {
            try {
                response = new String(response.getBytes("ISO-8859-1"), "utf-8");
                MainHomeRecommendBean bean = new Gson().fromJson(response, MainHomeRecommendBean.class);
                HomeMainRecommendAdapter adapter = new HomeMainRecommendAdapter(bean.getRows());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.get());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                recommendRecyclerView.setLayoutManager(linearLayoutManager);
                recommendRecyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, error -> {

        });
        queue.add(stringRequest);

    }

    private void getSpecial() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.URL_PRESS + "press/list?hot=Y", response -> {
            try {
                response = new String(response.getBytes("ISO-8859-1"), "utf-8");
                MainHomeSpecialBean bean = new Gson().fromJson(response, MainHomeSpecialBean.class);
                HomeMainSpecialAdapter adapter = new HomeMainSpecialAdapter(bean.getRows());
                GridLayoutManager gridLayoutManager = new GridLayoutManager(App.get(), 2);
                specialRecyclerView.setLayoutManager(gridLayoutManager);
                specialRecyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, error -> {

        });
        queue.add(stringRequest);

    }

    private void getNewsType() {

        RequestQueue queue = Volley.newRequestQueue(App.get());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constant.URL_PRESS + "category/list", response -> {
            try {
                response = new String(response.getBytes("ISO-8859-1"), "utf-8");
                MainHomeNewsTypeBean bean = new Gson().fromJson(response, MainHomeNewsTypeBean.class);
                HomeMainNewsTypeAdapter adapter = new HomeMainNewsTypeAdapter(bean.getData());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.get());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                newsTypeRecyclerView.setLayoutManager(linearLayoutManager);
                newsTypeRecyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, error -> {

        });
        queue.add(stringRequest);

    }

}
